import {
	BASE_URL
} from '@/config.js'
import {getToken, removeToken} from '@/utils/storageToken.js'
import {toPage} from '@/utils/tools.js'


/* 网络请求 */
const request = (options) => {
	const header = {...options.headers};
	const token = getToken();
	if(token) header.openid = token;
	return new Promise((resolve, reject) => {
		uni.request({
			url: BASE_URL + options.url,
			method: options.method || 'GET',
			data: {
				...options.data,
				openid: token
			},
			header,
			timeout: 30000,
			success: res => {
				uni.hideLoading()
				const data = res.data
				const {code} = data
				console.log(data);
				if (code == '0000') {
					//返回正常信息
					resolve(data)
				} else if(code == 401 && token) {
					removeToken();
					uni.showToast({
						title: '登录状态过期',
						icon: 'none',
						duration: 2000
					});
				}else {
					uni.showToast({
						title: data.msg ? data.msg : '服务器未知错误',
						icon: 'none',
						duration: 2000
					});
					//返回异常信息
					reject(data)
				}
			},
			fail: err => {
				uni.hideLoading()
				uni.showToast({
					icon: 'none',
					title: "请求超时,请稍后重试"
				})
				reject(err)
			}
		})
	})
}

export default request;